/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package interact;

import GUI.ShowMessageDialog;
import java.awt.Component;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.text.JTextComponent;


/**
 *
 * @author UnderGround
 */
public class ValidateData {
private static SimpleDateFormat format=new SimpleDateFormat("dd-MM-yyyy");
// static Date date1,date2;
 private static CallableStatement call;
 private static ResultSet rs;


 public static boolean checkDuplicateData(String item, String proc, String field) {
        int count = 0;
        Vector vt = new Vector();
          try {
            call = connect.connection().prepareCall("{call "+proc+"(?)}");
            call.setString(1, item);
             rs = call.executeQuery();
            while (rs.next()) {
                count = rs.getInt("count");
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
            System.out.println("error sql");
        }
        System.out.println(count);
        if(count>=1){
            return false;
        }else{
            return true;
        }

    }
 

public static boolean check_Status_To_Del(String procName,String id){
        int status =0;
        try {
            call = connect.connection().prepareCall("{call "+procName+"(?)}");
            call.setString(1, id);
             rs = call.executeQuery();
            while (rs.next()) {
                status = rs.getInt("status");
            }
        } catch (SQLException ex) {
            System.out.println("error sql");
        }
        if(status == 0){
            return false;
        }else{
             return true;
        }

}
public static boolean checkQuantity(String number) {
    int quantity = Integer.valueOf(number);
    if(quantity<=0){
        return false;
    }else{
        return true;
    }
}

    public static boolean checkDate(Date date1, Date date2) {
            if (date2.before(date1)){
                return false;
            } else {
                return true;
            }
    }

     public static Date convertStringToDate(String sDate)
    {
          SimpleDateFormat format=new SimpleDateFormat("dd-MM-yyyy");
        Date date=null;

        try {
             date = new Date(format.parse(sDate).getTime());
            
        } catch (ParseException ex) {
            ex.printStackTrace();
            System.out.println("Convert Fail");
        }
        finally {
            return date;
        }
    }

    public static String convertDateToString(Date date)
    {
        SimpleDateFormat format=new SimpleDateFormat("MM-dd-yyyy");
        return format.format(date);
    }
     public static String convertDateToString2(Date date)
    {
        SimpleDateFormat format=new SimpleDateFormat("dd-MM-yyyy");
        return format.format(date);
    }



 public static boolean checkEmpty(String text,Component component) {
        if (text.trim().length() == 0) {
            component.requestFocus();
            component.setBackground(new java.awt.Color(250,255,168));
            return false;
        } else {
             component.setBackground(java.awt.Color.white);
            return true;
        }
    }

 public static boolean isNumber(String value,JTextComponent component) {
        Pattern pattern = Pattern.compile("(\\d+)");
        Matcher matcher = pattern.matcher(value);
        if (!matcher.find()) {
            component.requestFocus();
            component.selectAll();
            component.setBackground(new java.awt.Color(250,255,168));
            
            return false;
        } else {
            component.setBackground(java.awt.Color.white);
            return true;
        }
    }
  public static boolean checkContent(String text,JTextComponent component)
    {
        if(!text.matches("^[a-zA-Z0-9]+$"))
        {
            component.requestFocus();
            component.selectAll();
            component.setBackground(new java.awt.Color(250,255,168));
            return false;
        }
        else{
            component.setBackground(java.awt.Color.white);
            return true;
        }
        
    }

  public static boolean isEmail(String value,JTextComponent component) {
        Pattern p = Pattern.compile("^[a-zA-Z]+((\\_|\\.)[a-zA-Z0-9]+)*\\@([a-zA-Z]+\\.)+[a-zA-Z]{2,4}$");
        Matcher m = p.matcher(value);
        if (!m.matches()) {
            component.requestFocus();
            component.selectAll();
            component.setBackground(new java.awt.Color(250,255,168));
            return false;
        } else {
            component.setBackground(java.awt.Color.white);
            return true;
        }
    }

   public static boolean isPhone(String value,JTextComponent component) {
        Pattern p = Pattern.compile("[0]{1}[1-9]{1}[0-9]{8,9}");
       Matcher m = p.matcher(value);
       if (!m.matches()) {
           component.requestFocus();
           component.selectAll();
           component.setBackground(new java.awt.Color(250, 255, 168));
           return false;

       } else {
           component.setBackground(java.awt.Color.white);
           return true;
       }
    }

     
    public static void showMessage(String message) {

        new ShowMessageDialog(null, true, message, "Validation Message", ShowMessageDialog.MESSAGE);
    }
     public static void showComfirmDialog(String message) {

        new ShowMessageDialog(null, true, message, "Validation Message", ShowMessageDialog.CONFIRM);
         if(ShowMessageDialog.STATUS == ShowMessageDialog.OK){
             System.out.println("ok");
         }
    }

     public static void main(String agrs[]){
         System.out.println( convertStringToDate("2-4-1999"));
         showComfirmDialog("fds");
         showMessage("fdsfdsf");
     }

}
